package net.forsalemall.android;

import org.json.JSONObject;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.Action;
import com.utils.Constants;
import com.utils.HttpUtils;
import com.utils.Utils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

public class BannerDetailsActivity extends Activity {

	private String bannerDetails;
	private String phoneNo;
	private ActionBar actionBar;
	private WebView webView;
	private ImageView imView;
	private String siteUrl;
	private String bigImgFileName;


	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.banner_details);
			webView = (WebView)findViewById(R.id.web_view);
//			webView.getSettings().setPluginsEnabled(true);
//			webView.getSettings().setUseWideViewPort(true);
			webView.getSettings().setJavaScriptEnabled(true);
			webView.getSettings().setBuiltInZoomControls(true);
			webView.setWebViewClient(new WebViewClient() {
				public boolean shouldOverrideUrlLoading(WebView view, String url) {
					webView.loadUrl(url);
					return false;	
				}
			});
			
			imView = (ImageView)findViewById(R.id.banner_image);
			bannerDetails = getIntent().getStringExtra(Constants.KEY_BANNER_DETAILS);
			JSONObject jo = new JSONObject(bannerDetails);
			phoneNo = jo.getString("pn");
			siteUrl = jo.getString("surl");
			bigImgFileName = jo.getString("bifn");
			actionBar = (ActionBar) findViewById(R.id.actionbar);
			actionBar.setTitle(phoneNo);

			actionBar.setHomeAction(new Action() {
				public void performAction(View view) {
					backToHome();
				}

				public int getDrawable() {
					// TODO Auto-generated method stub
					return R.drawable.ic_title_home_default;
				}
			});
			actionBar.addAction(new Action() {
				public void performAction(View view) {
					try {
						Intent intent = null;
						if (phoneNo != null) {
							intent = new Intent(Intent.ACTION_DIAL, Uri
									.parse("tel:" + phoneNo));
						} else {
							intent = new Intent(Intent.ACTION_DIAL);
						}
						startActivity(intent);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				public int getDrawable() {
					return R.drawable.ic_menu_call;
				}
			});
			actionBar.addAction(new Action() {
				public void performAction(View view) {
					try {
						Intent intent = null;
						if (phoneNo != null) {
							intent = new Intent(Intent.ACTION_SENDTO,
									Uri.parse("smsto:" + phoneNo));
						} else {
							intent = new Intent(Intent.ACTION_SENDTO);
						}
						startActivity(intent);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				public int getDrawable() {
					return R.drawable.ic_menu_send;
				}
			});
			
			actionBar.setDisplayHomeAsUpEnabled(true);
			if(siteUrl!=null && siteUrl.length()>5){
				webView.loadUrl(siteUrl);
				webView.setVisibility(View.VISIBLE);
			}else{
				loadImage();
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void backToHome() {
		try {
			startActivity(new Intent(this, HomeActivity.class)
					.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void loadImage() {
		new AsyncTask<String, String, String>() {
			private ProgressDialog progressDialog;
			private Bitmap bitmap;
			protected void onPreExecute() {
				try {
					progressDialog = Utils.getProgressDialog(
							BannerDetailsActivity.this, null,
							getString(R.string.loading));
					progressDialog.show();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			protected String doInBackground(String... arg0) {
				try {
					bitmap = HttpUtils.downloadFile(HttpUtils
							.getFileUrl(
									Constants.getBannerBigImgDirName(),
									bigImgFileName),
							BannerDetailsActivity.this);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}

			protected void onPostExecute(String unused) {
				try {
					if (bitmap != null) {
						imView.setImageBitmap(bitmap);
						imView.setVisibility(View.VISIBLE);
					}
					progressDialog.dismiss();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.execute();
	}
}
